package services

import (
	"errors"
	"server/global"
	"server/internal/model"
	"time"
)

var Menu = new(menu)

type menu struct {
	model.MenuRule
	Children []menu `json:"children,omitempty" gorm:"-"`
}

// GetTreeList 树状图
func (a menu) GetTreeList(pid int, rules, groupIds any) []menu {
	Db := global.GL_DB
	var menus []menu
	if rules != "*" {
		Db = Db.Where("id in (?)", groupIds)
	}
	Db.Model(&menus).Where("pid=? and status = ?", pid, 1).Order("weigh desc").Find(&menus)

	if len(menus) == 0 {
		return nil
	}
	for k, v := range menus {
		menus[k].Children = a.GetTreeList(v.ID, rules, groupIds)
	}

	return menus
}

func (a menu) GetList(rules, groupIds any) []menu {
	Db := global.GL_DB
	var menus []menu
	if rules != "*" {
		Db = Db.Where("id in (?)", groupIds)
	}
	Db.Model(&menus).Where("status=?", "1").Order("weigh").Find(&menus)

	return menus
}

// GetById 单条
func (a menu) GetById(id any) menu {
	Db := global.GL_DB
	detail := menu{}
	Db.Model(&a).Where("id =?", id).First(&detail)

	return detail

}

// Create 新增
func (a menu) Create(data model.MenuRule) (model.MenuRule, error) {
	Db := global.GL_DB

	t := time.Now()
	data.CreateTime = int(t.Unix())
	data.UpdateTime = int(t.Unix())
	Db = Db.Create(&data)
	return data, Db.Error
}

// Update 编辑
func (a menu) Update(data model.MenuRule) (model.MenuRule, error) {
	Db := global.GL_DB

	t := time.Now()
	data.UpdateTime = int(t.Unix())
	Db = Db.Updates(&data)
	return data, Db.Error

}

// Delete 删除
func (a menu) Delete(ids []int) error {
	Db := global.GL_DB

	var num int64
	Db.Table("system_menu").Where("pid in ?", ids).Count(&num)
	if num > 0 {
		return errors.New("存在关联数据无法删除")
	}

	return Db.Delete(&a, ids).Error

}

type selects struct {
	Value    int       `json:"value"`
	Label    string    `json:"label"`
	Children []selects `json:"children,omitempty"`
}

// GetSelect 远程下拉
func (a menu) GetSelect(rules, groupIds any) []menu {
	Db := global.GL_DB
	var menus []menu
	if rules != "*" {
		Db = Db.Where("id in (?)", groupIds)
	}
	Db.Model(&menus).Where("status=? and menu_type<>?", "1", 3).Order("weigh").Find(&menus)

	return menus
}

type MenuList struct {
	Path      string     `json:"path"`
	Name      string     `json:"name"`
	Meta      Mata       `json:"meta"`
	Component string     `json:"component"`
	Children  []MenuList `json:"children,omitempty"`
}
type Mata struct {
	Icon  string `json:"icon"`
	Title string `json:"title"`
	Roles string `json:"roles"`
	Rank  int    `json:"rank"`
}

// GetRouter 获取菜单
func (a menu) GetRouter(pid int, rules, groupIds any) []MenuList {
	Db := global.GL_DB
	var menus []menu
	if rules != "*" {
		Db = Db.Where("id in (?)", groupIds)
	}
	Db.Model(&menus).Where("pid=?", pid).Order("weigh").Find(&menus)

	if len(menus) == 0 {
		return nil
	}

	var lists []MenuList
	for _, v := range menus {
		lists = append(lists, MenuList{
			Path:      v.Path,
			Name:      v.Name,
			Component: v.Component,
			Meta:      Mata{Icon: v.Icon, Title: v.Title, Roles: "admin", Rank: 1},
			Children:  a.GetRouter(v.ID, rules, groupIds),
		})
	}
	return lists
}
